//张译文、曾凡诚
#include "ComponentPowerCurveDialog.h"
#include "ui_ComponentPowerCurveDialog.h"

ComponentPowerCurveDialog::ComponentPowerCurveDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ComponentPowerCurveDialog)
{
    ui->setupUi(this);
//张译文
    // 初始化曲线系列
    m_series = new QLineSeries(this);
    // 初始化图表
    m_chart = new QChart();
    m_chart->addSeries(m_series);
    m_chart->setTitle("组件功率曲线");
    // 初始化X轴（时间0-24小时）
    m_axisX = new QValueAxis();
    m_axisX->setTitleText("时间（小时）");
    m_axisX->setRange(0, 24);
    m_chart->addAxis(m_axisX, Qt::AlignBottom);
    m_series->attachAxis(m_axisX);
    // 初始化Y轴（功率范围，先设0-10，可根据数据自动调整）
    m_axisY = new QValueAxis();
    m_axisY->setTitleText("功率（kW）");
    m_axisY->setRange(0, 10);
    m_chart->addAxis(m_axisY, Qt::AlignLeft);
    m_series->attachAxis(m_axisY);
    // 设置图表到视图
    ui->powercurve->setChart(m_chart);
    ui->powercurve->setRenderHint(QPainter::Antialiasing); // 抗锯齿
}

ComponentPowerCurveDialog::~ComponentPowerCurveDialog()
{
    delete ui;
}
//张译文+曾凡诚
// 设置组件数据并绘制曲线
void ComponentPowerCurveDialog::setComponentData(const QMap<int, float>& powerMap, const QString& componentName)
{
    m_series->clear(); // 清空原有数据
    // 遍历24小时，从映射表取功率
    for (int hour = 0; hour < 24; ++hour) {
        float power = powerMap.value(hour, 0.0f); // 无数据时默认为0
        m_series->append(hour, power);
    }
    // 更新图表标题
    m_chart->setTitle(componentName + " 功率曲线");
    // （可选）自动调整Y轴范围为数据最大值+裕量
    qreal maxPower = 0;
    for (auto it = powerMap.begin(); it != powerMap.end(); ++it) {
        if (it.value() > maxPower) maxPower = it.value();
    }
    m_axisY->setRange(0, maxPower * 1.1); // 留10%裕量
}

// 关闭按钮槽函数
void ComponentPowerCurveDialog::on_btnClose_clicked()
{
    this->close();
}
